import pymysql
from db import db
conn=pymysql.connect(**db)
cursor=conn.cursor()

class Creat(object):
    @classmethod
    def create_database_bbs(self):  #创建数据库
        sql='''
        create database bbs default charset=utf8;
        '''
        cursor.execute(sql)
        conn.commit()

    @classmethod
    def switch(self):   #切换数据库
        sql='''
        use bbs;
        '''
        cursor.execute(sql)
        conn.commit()

    @classmethod
    def exists(self):   #判断库是否存在
        sql='''
        drop database if exists bbs;
        '''
        cursor.execute(sql)
        conn.commit()

    @classmethod
    def creat_table_user(self):        #创建user表
        sql = '''
        create table user(
        uid  int primary key auto_increment,
        username varchar(10) unique not null,
        usertype enum('普通用户','管理员'),
        password varchar(110) not null,
        regtime  datetime  default now(),
        email    varchar(20)
        );
        '''
        cursor.execute(sql)
        conn.commit()


if __name__=='__main__':
    try:
        Creat.exists()
        Creat.create_database_bbs()
        Creat.switch()
        Creat.creat_table_user()
    except Exception as e:
        print(e)
    finally:
        conn.close()
        cursor.close()


